<template>
	<om-addpage style="padding-top: 35px;" :busCode="busCode" :menuName="menuName" @formSubmit="formSubmit">
		<om-autoform ref="autoform" :initConfigData="mainConfigData" :initValue="initValue"></om-autoform>
	</om-addpage>
</template>

<script>
	import {
		errorModal,
		ignoreHtml,
		getUrl,
		confirmModal,
		ptFloat,
		minusF
	} from '@/static/js/common.js';
	import {
		changeConfigAttr,
		buildFormData,
		formValidation,
		tranferToEditors,
		changeEditor,
		setSubmitSubValue,
		configSelectData,
		hasValue,
		hasEditor,
		formShowLoading,
		getFormFields,
		formModelLoad,
		getWorkCacheKey
	} from '@/static/js/formUtil.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildFormData({})
		},
		computed: {
			...mapState(useLoginUserStore, ['userName', 'userId'])
		},
		methods: {
			formSubmit() {
				let _that = this
				_that.om.formSave("tcheckattence/save", _that.formdata, _that)
			},
			initLoad(options) {
				let _that = this
				let fileId = options.fileId
				let ajaxArr = [
					urlUtil.zposUrl(options.code)
				]
				if (fileId) {
					ajaxArr.push({
						url: "tcheckattence/getInfo",
						param: {
							id: fileId
						}
					})
				}
				formModelLoad(ajaxArr, function(res, mainData) {
					changeEditor("topinfo", "zposId", "data", configSelectData(res[0], "id", "posname"), mainData)
					if (_that.subConfigData["checkWay"]) {
						let configData = _that.subConfigData["checkWay"]
						let attr = {}
						if (hasEditor('checkWay', 'instId', configData)) {
							attr.titleTemplate = '机构:<%= instName %>'
						}
						if (hasEditor('checkWay', 'posId', configData)) {
							attr.subtitle1Template = '岗位:<%= posName %>'
						}
						if (hasEditor('checkWay', 'attenceCode', configData)) {
							attr.subtitle2Template = '考勤要求:<%= attenceName %>'
						}
						changeEditor('checkWay', 'objStr', attr, mainData);
					}
					if (fileId) {
						let fieldArr = getFormFields(mainData)
						fieldArr.push("id")
						for (let index in res[1].value.tcheckAttence) {
							if (fieldArr.includes(index)) {
								_that.$refs.autoform.$set(_that.$refs.autoform.formdata, index, res[1].value.tcheckAttence[index])
								_that.$set(_that.formdata, index, res[1].value.tcheckAttence[index])
							}
						}
						let dataList = _that._.cloneDeep(res[1].value.tcheckAttence_data)
						_that.subEditorObj["checkWay"].workList = dataList
						_that.$refs.autoform.$set(_that.$refs.autoform.formdata, 'checkWay', JSON.stringify(dataList))
						_that.$set(_that.formdata, 'checkWay', JSON.stringify(dataList))
						_that.$nextTick(function() {
							_that.$refs.autoform.initSelectIndex("topinfo", "zposId")
						})
					}
				}, options)
			}
		},
		onLoad(options) {
			let _that = this
			_that.initLoad(options)
		},
		onUnload() {
			//#ifdef APP-PLUS
			let _that = this;
			if (_that.$refs.autoform) {
				for (var fieldId in _that.subEditorObj) {
					uni.removeStorage({
						key: getWorkCacheKey(fieldId)
					});
				}
			}
			//#endif
		},
		onShow() {
			let _that = this
			if (_that.$refs.autoform) {
				for (let fileId in _that.subEditorObj) {
					let editor = _that.subEditorObj[fileId]
					uni.getStorage({
						key: getWorkCacheKey(fileId),
						success: (res) => {
							editor.workList = res.data
							uni.removeStorage({
								key: getWorkCacheKey(fileId)
							})
						}
					})
				}
			}
		}
	}
</script>

<style></style>
